//Komissarov Denis
package MAINWIN;

import java.awt.BorderLayout;
import java.awt.Insets;

import javax.swing.JDialog;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JPanel;
import javax.swing.UIManager;

import Resources.CEnvironment;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import API.CTask;

public class CTaskForm extends JDialog {

	private static final long serialVersionUID = 3052649293682392262L;
	final JTextArea textArea = new JTextArea();

	private CTask task;

	public CTaskForm(String title, final CTask task) {

		setSize(530, 400);
		setLocationRelativeTo(null);
		setResizable(isMinimumSizeSet());
		setTitle(title);
		getContentPane().setLayout(null);

		this.task = task;

		JButton btnOK = new JButton(CEnvironment.TaskOK);
		btnOK.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
				textArea.setEditable(false);
				setModal(false);
			}
		});
		btnOK.setBounds(425, 338, 89, 23);
		btnOK.setMargin(new Insets(2, 2, 2, 2));
		getContentPane().add(btnOK);

		JButton btnNext = new JButton(CEnvironment.TaskNext);
		btnNext.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		btnNext.setBounds(159, 338, 139, 23);
		btnNext.setMargin(new Insets(2, 2, 2, 2));
		btnNext.setVisible(false);
		getContentPane().add(btnNext);

		JButton btnBack = new JButton(CEnvironment.TaskBack);
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		btnBack.setBounds(10, 338, 139, 23);
		btnBack.setMargin(new Insets(2, 2, 2, 2));
		btnBack.setVisible(false);
		getContentPane().add(btnBack);

		JLabel lblTask = new JLabel(CEnvironment.TaskTask);
		lblTask.setBounds(10, 11, 212, 14);
		getContentPane().add(lblTask);

		JPanel panel = new JPanel();
		panel.setBorder(UIManager.getBorder("EditorPane.border"));
		panel.setBounds(10, 37, 504, 290);
		getContentPane().add(panel);
		panel.setLayout(new BorderLayout(0, 0));
		
		textArea.setLineWrap(true);
		textArea.setEditable(false);
		textArea.setWrapStyleWord(true);
		panel.add(new JScrollPane(textArea), BorderLayout.CENTER);

		addWindowListener(new WindowAdapter() {
			@Override
			public void windowOpened(WindowEvent arg0) {
				updateText();
			}
			@Override
			public void windowClosing(WindowEvent arg0) {
				textArea.setEditable(false);
				setModal(false);
			}
		});

	}
	
	protected void updateText() {
		if (task != null && textArea.getText() != task.getTask())
			textArea.setText(task.getTask());	
	}
	
	public void setTask(CTask task) {
		this.task = task;
		updateText();
	}
		
	public void setText() {
		textArea.setEditable(true);
	}
	
	public String getText() {
		return textArea.getText();
	}
}
